﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shop.entity;
using shop.util;
using System.Data.SqlClient;
using System.Data;
namespace shop.dao
{
    public class ImageDao
    {
        public Image GetImage(int id){
            string sql = "select id, name, imagePath,showStatus from image where id = @id";
            SqlParameter[] pms = { 
                                 new SqlParameter("@id", id),
                              
                                 };
            DataTable dt = SqlHelper.ExecuteDataTable(sql, pms);
           
            Image image  = new Image();
            if(dt.Rows.Count>0)
                EntityAdapt(image, dt.Rows[0]);
            return image;
        }
        public List<Image> ListImage()
        {
            string sql = "select id, name, imagePath,showStatus from image";
            DataTable dt = SqlHelper.ExecuteDataTable(sql, null);
            List<Image> imageList = new List<Image>();
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                Image image = new Image();
                EntityAdapt(image, dt.Rows[i]);
                imageList.Add(image);
            }

            return imageList;
        }
          private void EntityAdapt(Image Image,DataRow row){
              Image.Id = (int)row["id"];
              Image.Name = (string)row["name"];
              Image.ImagePath = (string)row["imagePath"];
          }
      
        public int AddImage(Image image) {
            string sql = "insert into image(name,imagePath,showStatus) values(@name,@imagePath,1) select @@identity  ";
            SqlParameter[] pms = { 
                                 new SqlParameter("@name", image.Name),
                                 new SqlParameter("@imagePath", image.ImagePath)
                                 };
            int Gid = SqlHelper.getInsertId(sql,pms);
            
            return Gid;         
        }

        public bool UpdateImage(Image image)
        {
            string sql = "update image set name=@name,showStatus = @showStatus              where id =@id";
            SqlParameter[] pms = { new SqlParameter("@name", image.Name),
                            new SqlParameter("@showStatus", image.showStatus),
                            new SqlParameter("@id", image.Id)
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql, pms);
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        public bool DeleteImage(int id)
        {
            string sql = "delete from image where id = @id";
            SqlParameter[] pms = { new SqlParameter("@id", id) };
            int count = 0;
            try {
             count = SqlHelper.ExecuteNonQuery(sql, pms);
            }
            catch(Exception e) {
                Console.Write(e.Message);

            }
            if (count > 0)
            {
                return true;
            }
            return false;
        }
    }
}